//
//  DxViewController.m
//  ObjcGoNew
//
//  Created by 邢伟新 on 2021/4/26.
//


#import "DxViewController.h"
#import <objc/runtime.h> //包含对类、成员变量、属性、方法的操作
 #import <objc/message.h> //包含消息机制

#pragma mark - person类
@interface Person : NSObject
{
    int _age;
}

@end
@implementation Person
@end

#pragma mark - student类

@interface Student : Person
{
    int _no; //编号
}

@end
@implementation Student
@end

#pragma mark -控制器


@interface DxViewController ()

@end

@implementation DxViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"对象的本质";
    self.view.backgroundColor = [UIColor whiteColor];
    
    //获取对象占用内存
//    [self getSizeOfObject];
    
    //通过对象转化成类
    [self getClassByObject];
    
}

-(void)getSizeOfObject{
    
    NSObject *obj = [[NSObject alloc]init];
    Person *person = [[Person alloc]init];
    Student *stu = [[Student alloc]init];
    
    
    //创建Student对象首先会分配16个字节，存储3个东西，isa指针8个字节，4个字节的_no ,4个字节的_age
    NSLog(@"obj占用%zd字节,person占用%zd,stu占用%zd字节",class_getInstanceSize([NSObject class]),class_getInstanceSize([Person class]),class_getInstanceSize([Student class]));
    
    
    /*
     内存对齐的两大原则
      原则 1. 前面的地址必须是后面的地址正数倍,不是就补齐。
      原则 2. 整个Struct的地址必须是最大字节的整数倍。
     */
    
    
}

-(void)getClassByObject{
    NSObject *obj = [[NSObject alloc]init];
    Person *person = [[Person alloc]init];
    Student *stu = [[Student alloc]init];
    
    
    Class C1 = [obj class];
    Class C2 = [person class];
    Class C3 = [stu class];
    
    Class C4 = object_getClass(obj);
    Class C5 = object_getClass(person);
    Class C6 = object_getClass(stu);
    
    NSString *s1 = NSStringFromClass(C4);
    NSString *s2 = NSStringFromClass(C5);
    NSString *s3 = NSStringFromClass(C6);
    
    
    NSLog(@"%@----%@-----%@",C1,C2,C3);
    NSLog(@"%p----%p-----%p",C4,C5,C6);
    NSLog(@"%@----%@-----%@",s1,s2,s3);
    
    
}


@end
